
** ---------------------------------- **
* Author: Abdulrazzak Tamim 
* First version: 11/8/2020
* Last modified: 26/4/2024							
** ---------------------------------- **

* User must define a global macro in order to run the program:												
* (1) "path" is the raw data folder											
												
												
clear all												
set more off												
program drop _all												
												
if "`c(username)'"=="abdulrazzaktamim" {												
gl path "/Users/abdulrazzaktamim/Library/CloudStorage/Dropbox/SoilDoc-Abdulrazzak"												
}												
																					
program main	
	
	use "$path/replication/data/gps_yields.dta", clear
	merge m:1 respondent_id using "$path/replication/data/controls.dta", nogen /// 
	keepusing(headage malehead headeduc headeducp hhsize asstfullsc_qrt)	
	
	mat A_6 = J(5,1,.) 
	mat A_9 = J(5,1,.) 
	mat B_6 = J(5,1,.) 
	mat B_9 = J(5,1,.) 
	mat C_6 = J(5,1,.) 
	mat C_9 = J(5,1,.) 
	mat D_6 = J(5,1,.) 
	mat D_9 = J(5,1,.) 
	mat E_6 = J(5,1,.) 
	mat E_9 = J(5,1,.) 
	mat F_6 = J(5,1,.) 
	mat F_9 = J(5,1,.) 	
	
	preserve
		drop if trt==5
		IPW
		ITT_IPW
		ITT
		ITT_Spec2
		Pooling
		Scatter
	restore
	
	CrossVillageITT	
end


program IPW 

	g missing=. 
	
	forv i=4/9 { 
		if inlist(5,7,8,`i') continue
		g missing_201`i' = (mi(far_mz_kg_acre_q30_) | mi(fr_baseline) | mi(gps_mz_kg_acre_q30_) /// 
		| mi(gps_baseline)) & year==201`i' if year==201`i'
		replace missing=missing_201`i' if year==201`i'
	}

	ta missing_2014, m 
	ta missing_2016, m 
	ta missing_2019, m 
	ta missing, m 
	
	probit missing i.trt##(c.headage i.malehead i.headeducp c.hhsize i.asstfullsc_qrt) i.village if year==2016, r cl(village) asis
	outreg2 using "$path/replication/output/TableA6.doc", append bd(2) sd(2) nocons addtext(Village FE, YES) label ctitle(sr_gps_16) /// 
	keep(2.trt##c.headage 2.trt##1.malehead 2.trt##1.headeducp 2.trt##c.hhsize 2.trt##2.asstfullsc_qrt 2.trt##3.asstfullsc_qrt 2.trt##4.asstfullsc_qrt 3.trt##c.headage 3.trt##1.malehead 3.trt##1.headeducp 3.trt##c.hhsize 3.trt##2.asstfullsc_qrt 3.trt##3.asstfullsc_qrt 3.trt##4.asstfullsc_qrt 4.trt##c.headage 4.trt##1.malehead 4.trt##1.headeducp 4.trt##c.hhsize 4.trt##2.asstfullsc_qrt 4.trt##3.asstfullsc_qrt 4.trt##4.asstfullsc_qrt)
	predict probhat_2016
	as inrange(probhat_2016,0,1) if missing_2016!=.
	gen ipw_2016 = 1/probhat_2016
	
	probit missing i.trt##(c.headage i.malehead i.headeducp c.hhsize i.asstfullsc_qrt) i.village if year==2019, r cl(village) asis
	outreg2 using "$path/replication/output/TableA6.doc", append bd(2) sd(2) nocons addtext(Village FE, YES) label ctitle(sr_gps_19) /// 
	keep(2.trt##c.headage 2.trt##1.malehead 2.trt##1.headeducp 2.trt##c.hhsize 2.trt##2.asstfullsc_qrt 2.trt##3.asstfullsc_qrt 2.trt##4.asstfullsc_qrt 3.trt##c.headage 3.trt##1.malehead 3.trt##1.headeducp 3.trt##c.hhsize 3.trt##2.asstfullsc_qrt 3.trt##3.asstfullsc_qrt 3.trt##4.asstfullsc_qrt 4.trt##c.headage 4.trt##1.malehead 4.trt##1.headeducp 4.trt##c.hhsize 4.trt##2.asstfullsc_qrt 4.trt##3.asstfullsc_qrt 4.trt##4.asstfullsc_qrt)
	predict probhat_2019
	as inrange(probhat_2019,0,1) if missing_2019!=.
	gen ipw_2019 = 1/probhat_2019
	
	g ipw=ipw_2016 if year==2016
	replace ipw=ipw_2019 if year==2019
	
	ta ipw 
	erase "$path/replication/output/TableA6.txt"

end


program ITT_IPW 

	areg far_mz_kg_acre_q30_ i.trt2 fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& year==2016 [pw=ipw], a(village) r cl(village) 
			outreg2 using "$path/replication/output/Table6.doc", append /// 
			ctitle(fr6) bd(2) sd(2) nocons addtext(Village FE, YES)	
        su fr_baseline if e(sample) & trt2 == 1		
		mat A_6[1,1] = r(mean)
		mat A_6[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat A_6[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat A_6[4,1] = r(p)
		test 3.trt2=4.trt2
		mat A_6[5,1] = r(p)
		mat list A_6
		
	areg gps_mz_kg_acre_q30_ i.trt2 gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& year==2016 [pw=ipw], a(village) r cl(village) 
			outreg2 using "$path/replication/output/Table6.doc", append /// 
			ctitle(gps6) bd(2) sd(2) nocons addtext(Village FE, YES)	
        su gps_baseline if e(sample) & trt2 == 1		
		mat B_6[1,1] = r(mean)
		mat B_6[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat B_6[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat B_6[4,1] = r(p)
		test 3.trt2=4.trt2
		mat B_6[5,1] = r(p)
		mat list B_6	
		
	areg far_mz_kg_acre_q30_ i.trt2 fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& year==2019 [pw=ipw], a(village) r cl(village) 
			outreg2 using "$path/replication/output/Table6.doc", append /// 
			ctitle(fr9) bd(2) sd(2) nocons addtext(Village FE, YES)	
        su fr_baseline if e(sample) & trt2 == 1		
		mat A_9[1,1] = r(mean)
		mat A_9[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat A_9[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat A_9[4,1] = r(p)
		test 3.trt2=4.trt2
		mat A_9[5,1] = r(p)
		mat list A_9
		
	areg gps_mz_kg_acre_q30_ i.trt2 gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& year==2019 [pw=ipw], a(village) r cl(village) 
			outreg2 using "$path/replication/output/Table6.doc", append /// 
			ctitle(gps9) bd(2) sd(2) nocons addtext(Village FE, YES)	
        su gps_baseline if e(sample) & trt2 == 1		
		mat B_9[1,1] = r(mean)
		mat B_9[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat B_9[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat B_9[4,1] = r(p)
		test 3.trt2=4.trt2
		mat B_9[5,1] = r(p)
		mat list B_9	
		
	areg far_mz_kg_acre_q30_ i.trt2 fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& gps_area_old_>0.5 & year==2016 [pw=ipw], a(village) r cl(village) 
			outreg2 using "$path/replication/output/Table6.doc", append /// 
			ctitle(fr6_0.5) bd(2) sd(2) nocons addtext(Village FE, YES)	
        su fr_baseline if e(sample) & trt2 == 1		
		mat E_6[1,1] = r(mean)
		mat E_6[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat E_6[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat E_6[4,1] = r(p)
		test 3.trt2=4.trt2
		mat E_6[5,1] = r(p)
		mat list E_6
		
	areg gps_mz_kg_acre_q30_ i.trt2 gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& gps_area_old_>0.5 & year==2016 [pw=ipw], a(village) r cl(village) 
			outreg2 using "$path/replication/output/Table6.doc", append /// 
			ctitle(gps6_0.5) bd(2) sd(2) nocons addtext(Village FE, YES)
        su gps_baseline if e(sample) & trt2 == 1		
		mat F_6[1,1] = r(mean)
		mat F_6[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat F_6[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat F_6[4,1] = r(p)
		test 3.trt2=4.trt2
		mat F_6[5,1] = r(p)
		mat list F_6	
		
	areg far_mz_kg_acre_q30_ i.trt2 fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& gps_area_old_>0.5 & year==2019 [pw=ipw], a(village) r cl(village) 
			outreg2 using "$path/replication/output/Table6.doc", append /// 
			ctitle(fr9_0.5) bd(2) sd(2) nocons addtext(Village FE, YES)	
        su fr_baseline if e(sample) & trt2 == 1		
		mat E_9[1,1] = r(mean)
		mat E_9[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat E_9[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat E_9[4,1] = r(p)
		test 3.trt2=4.trt2
		mat E_9[5,1] = r(p)
		mat list E_9
		
	areg gps_mz_kg_acre_q30_ i.trt2 gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& gps_area_old_>0.5 & year==2019 [pw=ipw], a(village) r cl(village) 
			outreg2 using "$path/replication/output/Table6.doc", append /// 
			ctitle(gps9_0.5) bd(2) sd(2) nocons addtext(Village FE, YES)
        su gps_baseline if e(sample) & trt2 == 1		
		mat F_9[1,1] = r(mean)
		mat F_9[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat F_9[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat F_9[4,1] = r(p)
		test 3.trt2=4.trt2
		mat F_9[5,1] = r(p)
		mat list F_9	
		
	
	*Export
	matmap A_6 A_6N, map(round(@,0.01))
	matmap A_9 A_9N, map(round(@,0.01))
	matmap B_6 B_6N, map(round(@,0.01))
	matmap B_9 B_9N, map(round(@,0.01))
	matmap E_6 E_6N, map(round(@,0.01))
	matmap E_9 E_9N, map(round(@,0.01))
	matmap F_6 F_6N, map(round(@,0.01))
	matmap F_9 F_9N, map(round(@,0.01))
	
	putexcel set "$path/replication/output/Table6_F_tests.xls", sheet("F-tests") replace
	putexcel B2=matrix(A_6N) 
	putexcel C2=matrix(B_6N)
	putexcel D2=matrix(A_9N)
	putexcel E2=matrix(B_9N)		
	putexcel F2=matrix(E_6N) 
	putexcel G2=matrix(F_6N)
	putexcel H2=matrix(E_9N)
	putexcel I2=matrix(F_9N)	
	
	putexcel B1 = "SR_16"
	putexcel C1 = "GPS_16"
	putexcel D1 = "SR_19"
	putexcel E1 = "GPS_19"
	putexcel F1 = "SR_16"
	putexcel G1 = "GPS_16"
	putexcel H1 = "SR_19"
	putexcel I1 = "GPS_19"
	putexcel A2 = "CONTROL MEAN"
	putexcel A3 = "CONTROL SD"
	putexcel A4 = "V_vs_R"
	putexcel A5 = "V_vs_RV"
	putexcel A6 = "R_vs_RV"
	putexcel A7 = "less than 0.5 acres"
	putexcel B7 = "NO"
	putexcel C7 = "NO"
	putexcel D7 = "NO"
	putexcel E7 = "NO"
	putexcel F7 = "YES"
	putexcel G7 = "YES"
	putexcel H7 = "YES"
	putexcel I7 = "YES"
	
	erase "$path/replication/output/Table6.txt"

end


program ITT
	
	areg far_mz_kg_acre_q30_ i.trt2 fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& year==2016, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA12.doc", append /// 
			ctitle(fr6) bd(2) sd(2) nocons addtext(Village FE, YES)	
        su fr_baseline if e(sample) & trt2 == 1		
		mat A_6[1,1] = r(mean)
		mat A_6[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat A_6[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat A_6[4,1] = r(p)
		test 3.trt2=4.trt2
		mat A_6[5,1] = r(p)
		mat list A_6
		
	areg gps_mz_kg_acre_q30_ i.trt2 gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& year==2016, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA12.doc", append /// 
			ctitle(gps6) bd(2) sd(2) nocons addtext(Village FE, YES)	
        su gps_baseline if e(sample) & trt2 == 1		
		mat B_6[1,1] = r(mean)
		mat B_6[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat B_6[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat B_6[4,1] = r(p)
		test 3.trt2=4.trt2
		mat B_6[5,1] = r(p)
		mat list B_6	
		
	areg far_mz_kg_acre_q30_ i.trt2 fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& year==2019, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA12.doc", append /// 
			ctitle(fr9) bd(2) sd(2) nocons addtext(Village FE, YES)	
        su fr_baseline if e(sample) & trt2 == 1		
		mat A_9[1,1] = r(mean)
		mat A_9[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat A_9[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat A_9[4,1] = r(p)
		test 3.trt2=4.trt2
		mat A_9[5,1] = r(p)
		mat list A_9
		
	areg gps_mz_kg_acre_q30_ i.trt2 gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& year==2019, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA12.doc", append /// 
			ctitle(gps9) bd(2) sd(2) nocons addtext(Village FE, YES)	
        su gps_baseline if e(sample) & trt2 == 1		
		mat B_9[1,1] = r(mean)
		mat B_9[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat B_9[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat B_9[4,1] = r(p)
		test 3.trt2=4.trt2
		mat B_9[5,1] = r(p)
		mat list B_9	
		
	areg far_mz_kg_acre_q30_ i.trt2 fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& gps_area_old_>0.5 & year==2016, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA12.doc", append /// 
			ctitle(fr6_0.5) bd(2) sd(2) nocons addtext(Village FE, YES)	
        su fr_baseline if e(sample) & trt2 == 1		
		mat E_6[1,1] = r(mean)
		mat E_6[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat E_6[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat E_6[4,1] = r(p)
		test 3.trt2=4.trt2
		mat E_6[5,1] = r(p)
		mat list E_6
		
	areg gps_mz_kg_acre_q30_ i.trt2 gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& gps_area_old_>0.5 & year==2016, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA12.doc", append /// 
			ctitle(gps6_0.5) bd(2) sd(2) nocons addtext(Village FE, YES)
        su gps_baseline if e(sample) & trt2 == 1		
		mat F_6[1,1] = r(mean)
		mat F_6[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat F_6[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat F_6[4,1] = r(p)
		test 3.trt2=4.trt2
		mat F_6[5,1] = r(p)
		mat list F_6	
		
	areg far_mz_kg_acre_q30_ i.trt2 fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& gps_area_old_>0.5 & year==2019, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA12.doc", append /// 
			ctitle(fr9_0.5) bd(2) sd(2) nocons addtext(Village FE, YES)	
        su fr_baseline if e(sample) & trt2 == 1		
		mat E_9[1,1] = r(mean)
		mat E_9[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat E_9[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat E_9[4,1] = r(p)
		test 3.trt2=4.trt2
		mat E_9[5,1] = r(p)
		mat list E_9
		
	areg gps_mz_kg_acre_q30_ i.trt2 gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& gps_area_old_>0.5 & year==2019, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA12.doc", append /// 
			ctitle(gps9_0.5) bd(2) sd(2) nocons addtext(Village FE, YES)
        su gps_baseline if e(sample) & trt2 == 1		
		mat F_9[1,1] = r(mean)
		mat F_9[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat F_9[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat F_9[4,1] = r(p)
		test 3.trt2=4.trt2
		mat F_9[5,1] = r(p)
		mat list F_9	
		
	erase "$path/replication/output/TableA12.txt"
	
	*Export
	matmap A_6 A_6N, map(round(@,0.01))
	matmap A_9 A_9N, map(round(@,0.01))
	matmap B_6 B_6N, map(round(@,0.01))
	matmap B_9 B_9N, map(round(@,0.01))
	matmap E_6 E_6N, map(round(@,0.01))
	matmap E_9 E_9N, map(round(@,0.01))
	matmap F_6 F_6N, map(round(@,0.01))
	matmap F_9 F_9N, map(round(@,0.01))
	
	putexcel set "$path/replication/output/TableA12_F_tests.xls", sheet("F-tests") replace
	putexcel B2=matrix(A_6N) 
	putexcel C2=matrix(B_6N)
	putexcel D2=matrix(A_9N)
	putexcel E2=matrix(B_9N)		
	putexcel F2=matrix(E_6N) 
	putexcel G2=matrix(F_6N)
	putexcel H2=matrix(E_9N)
	putexcel I2=matrix(F_9N)	
	
	putexcel B1 = "SR_16"
	putexcel C1 = "GPS_16"
	putexcel D1 = "SR_19"
	putexcel E1 = "GPS_19"
	putexcel F1 = "SR_16"
	putexcel G1 = "GPS_16"
	putexcel H1 = "SR_19"
	putexcel I1 = "GPS_19"
	putexcel A2 = "CONTROL MEAN"
	putexcel A3 = "CONTROL SD"
	putexcel A4 = "V_vs_R"
	putexcel A5 = "V_vs_RV"
	putexcel A6 = "R_vs_RV"
	putexcel A7 = "less than 0.5 acres"
	putexcel B7 = "NO"
	putexcel C7 = "NO"
	putexcel D7 = "NO"
	putexcel E7 = "NO"
	putexcel F7 = "YES"
	putexcel G7 = "YES"
	putexcel H7 = "YES"
	putexcel I7 = "YES"
	
end


program ITT_Spec2

	g V=trt2==2|trt2==4 if trt2!=.
	g R=trt2==3|trt2==4 if trt2!=.
	
	areg far_mz_kg_acre_q30_ i.V##i.R fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& year==2016, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA15.doc", append /// 
			ctitle(fr6) bd(2) sd(2) nocons addtext(Village FE, YES)	/// 
		keep(1.V 1.R 1.V#1.R fr_baseline)
        su fr_baseline if e(sample) & trt2 == 1		
		mat A_6[1,1] = r(mean)
		mat A_6[2,1] = r(sd)
		test 1.V=1.R
		mat A_6[3,1] = r(p)  
		test 1.V=1.R#1.V
		mat A_6[4,1] = r(p)
		test 1.R=1.R#1.V
		mat A_6[5,1] = r(p)
		mat list A_6
		
	areg gps_mz_kg_acre_q30_ i.V##i.R gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& year==2016, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA15.doc", append /// 
			ctitle(gps6) bd(2) sd(2) nocons addtext(Village FE, YES) /// 
		keep(1.V 1.R 1.V#1.R gps_baseline)
        su gps_baseline if e(sample) & trt2 == 1		
		mat B_6[1,1] = r(mean)
		mat B_6[2,1] = r(sd)
		test 1.V=1.R
		mat B_6[3,1] = r(p)  
		test 1.V=1.R#1.V
		mat B_6[4,1] = r(p)
		test 1.R=1.R#1.V
		mat B_6[5,1] = r(p)
		mat list B_6	
		
	areg far_mz_kg_acre_q30_ i.V##i.R fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& year==2019, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA15.doc", append /// 
			ctitle(fr9) bd(2) sd(2) nocons addtext(Village FE, YES) /// 
		keep(1.V 1.R 1.V#1.R fr_baseline)	
        su fr_baseline if e(sample) & trt2 == 1		
		mat A_9[1,1] = r(mean)
		mat A_9[2,1] = r(sd)
		test 1.V=1.R
		mat A_9[3,1] = r(p)  
		test 1.V=1.R#1.V
		mat A_9[4,1] = r(p)
		test 1.R=1.R#1.V
		mat A_9[5,1] = r(p)
		mat list A_9
		
	areg gps_mz_kg_acre_q30_ i.V##i.R gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& year==2019, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA15.doc", append /// 
			ctitle(gps9) bd(2) sd(2) nocons addtext(Village FE, YES) /// 
		keep(1.V 1.R 1.V#1.R gps_baseline)	
        su gps_baseline if e(sample) & trt2 == 1		
		mat B_9[1,1] = r(mean)
		mat B_9[2,1] = r(sd)
		test 1.V=1.R
		mat B_9[3,1] = r(p)  
		test 1.V=1.R#1.V
		mat B_9[4,1] = r(p)
		test 1.R=1.R#1.V
		mat B_9[5,1] = r(p)
		mat list B_9	
		
	areg far_mz_kg_acre_q30_ i.V##i.R fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& gps_area_old_>0.5 & year==2016, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA15.doc", append /// 
			ctitle(fr6_0.5) bd(2) sd(2) nocons addtext(Village FE, YES) /// 
		keep(1.V 1.R 1.V#1.R fr_baseline)	
        su fr_baseline if e(sample) & trt2 == 1		
		mat E_6[1,1] = r(mean)
		mat E_6[2,1] = r(sd)
		test 1.V=1.R
		mat E_6[3,1] = r(p)  
		test 1.V=1.R#1.V
		mat E_6[4,1] = r(p)
		test 1.R=1.R#1.V
		mat E_6[5,1] = r(p)
		mat list E_6
		
	areg gps_mz_kg_acre_q30_ i.V##i.R gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& gps_area_old_>0.5 & year==2016, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA15.doc", append /// 
			ctitle(gps6_0.5) bd(2) sd(2) nocons addtext(Village FE, YES) /// 
		keep(1.V 1.R 1.V#1.R gps_baseline)
        su gps_baseline if e(sample) & trt2 == 1		
		mat F_6[1,1] = r(mean)
		mat F_6[2,1] = r(sd)
		test 1.V=1.R
		mat F_6[3,1] = r(p)  
		test 1.V=1.R#1.V
		mat F_6[4,1] = r(p)
		test 1.R=1.R#1.V
		mat F_6[5,1] = r(p)
		mat list F_6	
		
	areg far_mz_kg_acre_q30_ i.V##i.R fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& gps_area_old_>0.5 & year==2019, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA15.doc", append /// 
			ctitle(fr9_0.5) bd(2) sd(2) nocons addtext(Village FE, YES) /// 
		keep(1.V 1.R 1.V#1.R fr_baseline)	
        su fr_baseline if e(sample) & trt2 == 1		
		mat E_9[1,1] = r(mean)
		mat E_9[2,1] = r(sd)
		test 1.V=1.R
		mat E_9[3,1] = r(p)  
		test 1.V=1.R#1.V
		mat E_9[4,1] = r(p)
		test 1.R=1.R#1.V
		mat E_9[5,1] = r(p)
		mat list E_9
		
	areg gps_mz_kg_acre_q30_ i.V##i.R gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& gps_area_old_>0.5 & year==2019, a(village) r cl(village) 
			outreg2 using "$path/replication/output/TableA15.doc", append /// 
			ctitle(gps9_0.5) bd(2) sd(2) nocons addtext(Village FE, YES) /// 
		keep(1.V 1.R 1.V#1.R gps_baseline)
        su gps_baseline if e(sample) & trt2 == 1		
		mat F_9[1,1] = r(mean)
		mat F_9[2,1] = r(sd)
		test 1.V=1.R
		mat F_9[3,1] = r(p)  
		test 1.V=1.R#1.V
		mat F_9[4,1] = r(p)
		test 1.R=1.R#1.V
		mat F_9[5,1] = r(p)
		mat list F_9	
		
	erase "$path/replication/output/TableA15.txt"
	*Export
	matmap A_6 A_6N, map(round(@,0.01))
	matmap A_9 A_9N, map(round(@,0.01))
	matmap B_6 B_6N, map(round(@,0.01))
	matmap B_9 B_9N, map(round(@,0.01))
	matmap E_6 E_6N, map(round(@,0.01))
	matmap E_9 E_9N, map(round(@,0.01))
	matmap F_6 F_6N, map(round(@,0.01))
	matmap F_9 F_9N, map(round(@,0.01))
	
	putexcel set "$path/replication/output/TableA15_F_tests.xls", sheet("F-tests") replace
	putexcel B2=matrix(A_6N) 
	putexcel C2=matrix(B_6N)
	putexcel D2=matrix(A_9N)
	putexcel E2=matrix(B_9N)		
	putexcel F2=matrix(E_6N) 
	putexcel G2=matrix(F_6N)
	putexcel H2=matrix(E_9N)
	putexcel I2=matrix(F_9N)
	
	putexcel B1 = "SR_16"
	putexcel C1 = "GPS_16"
	putexcel D1 = "SR_19"
	putexcel E1 = "GPS_19"
	putexcel F1 = "SR_16"
	putexcel G1 = "GPS_16"
	putexcel H1 = "SR_19"
	putexcel I1 = "GPS_19"
	putexcel A2 = "CONTROL MEAN"
	putexcel A3 = "CONTROL SD"
	putexcel A4 = "V_vs_R"
	putexcel A5 = "V_vs_RV"
	putexcel A6 = "R_vs_RV"
	putexcel A7 = "less than 0.5 acres"
	putexcel B7 = "NO"
	putexcel C7 = "NO"
	putexcel D7 = "NO"
	putexcel E7 = "NO"
	putexcel F7 = "YES"
	putexcel G7 = "YES"
	putexcel H7 = "YES"
	putexcel I7 = "YES"
	
end


program Pooling

	mat A_6 = J(2,1,.) 
	mat A_9 = J(2,1,.) 
	mat B_6 = J(2,1,.) 
	mat B_9 = J(2,1,.) 
	mat C_6 = J(2,1,.) 
	mat C_9 = J(2,1,.) 
	mat D_6 = J(2,1,.) 
	mat D_9 = J(2,1,.) 
	mat E_6 = J(2,1,.) 
	mat E_9 = J(2,1,.)  
	mat F_6 = J(2,1,.)  
	mat F_9 = J(2,1,.) 
	mat G_6 = J(2,1,.) 
	mat G_9 = J(2,1,.) 
	mat H_6 = J(2,1,.) 
	mat H_9 = J(2,1,.) 
	
	g trt3=(trt2==2 | trt2==4)
	
	forval i=6/9 {			
	if inlist(`i', 7, 8) continue			
	xtreg far_mz_kg_acre_q30_ i.trt3 fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 		
		& year==201`i', fe robust cl(vil) 	
			outreg2 using "$path/replication/output/TableA9.doc", append /// 
			ctitle(fr`i') bd(2) sd(2) nocons addtext(Village FE, YES)
        su fr_baseline if e(sample) & trt3 == 0	
		mat A_`i'[1,1] = r(mean)
		mat A_`i'[2,1] = r(sd)
		
	xtreg gps_mz_kg_acre_q30_  i.trt3 gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 		
		& year==201`i', fe robust cl(vil) 	
			outreg2 using "$path/replication/output/TableA9.doc", append /// 
			ctitle(gps`i') bd(2) sd(2) nocons addtext(Village FE, YES)
        su gps_baseline if e(sample) & trt3 == 0		
		mat B_`i'[1,1] = r(mean)
		mat B_`i'[2,1] = r(sd)
		mat list B_`i'
		
	xtreg far_mz_kg_acre_q30_ i.trt3 fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 		
		& gps_area_old_>0.5 & year==201`i', fe robust cl(vil) 	
			outreg2 using "$path/replication/output/TableA9.doc", append /// 
			ctitle(fr`i'0.5) bd(2) sd(2) nocons addtext(Village FE, YES)
        su fr_baseline if e(sample) & trt3 == 0		
		mat C_`i'[1,1] = r(mean)
		mat C_`i'[2,1] = r(sd)
		mat list C_`i'
		
	xtreg gps_mz_kg_acre_q30_ i.trt3 gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 		
		& gps_area_old_>0.5 & year==201`i', fe robust cl(vil) 	
			outreg2 using "$path/replication/output/TableA9.doc", append /// 
			ctitle(gps`i'0.5) bd(2) sd(2) nocons addtext(Village FE, YES)
        su gps_baseline if e(sample) & trt3 == 0			
		mat D_`i'[1,1] = r(mean)
		mat D_`i'[2,1] = r(sd)
		mat list D_`i'
		
	}	
	erase "$path/replication/output/TableA9.txt"

	*Export
	matmap A_6 A_6N, map(round(@,0.01))
	matmap A_9 A_9N, map(round(@,0.01))
	matmap B_6 B_6N, map(round(@,0.01))
	matmap B_9 B_9N, map(round(@,0.01))
	matmap C_6 C_6N, map(round(@,0.01))
	matmap C_9 C_9N, map(round(@,0.01))
	matmap D_6 D_6N, map(round(@,0.01))
	matmap D_9 D_9N, map(round(@,0.01))

	
	putexcel set "$path/replication/output/TableA9_F_tests.xls", sheet("F-tests") replace
	putexcel B2=matrix(A_6N) 
	putexcel C2=matrix(B_6N)
	putexcel D2=matrix(A_9N)
	putexcel E2=matrix(B_9N)		
	putexcel F2=matrix(C_6N) 
	putexcel G2=matrix(D_6N)
	putexcel H2=matrix(C_9N)
	putexcel I2=matrix(D_9N)
	
	putexcel B1 = "SR_16"
	putexcel C1 = "GPS_16"
	putexcel D1 = "SR_19"
	putexcel E1 = "GPS_19"
	putexcel F1 = "SR_16"
	putexcel G1 = "GPS_16"
	putexcel H1 = "SR_19"
	putexcel I1 = "GPS_19"
	putexcel A2 = "CONTROL MEAN"
	putexcel A3 = "CONTROL SD"
	putexcel A4 = "less than 0.5 acres"
	putexcel B4 = "NO"
	putexcel C4 = "NO"
	putexcel D4 = "NO"
	putexcel E4 = "NO"
	putexcel F4 = "YES"
	putexcel G4 = "YES"
	putexcel H4 = "YES"
	putexcel I4 = "YES"
		
end


program Scatter

	preserve
	drop if trt==5
	la var far_area_acre_ "SR"
	la var gps_area_old_ "GPS"
	forv i=1/4 {
		forv j=4/9 { 
		if inlist(`j',5,7,8) continue
		if `i'==1 { 
			correlate far_area_acre_ gps_area_old_ if trt2==`i' & year==201`j'
			loc rho: di %5.2f r(rho) 
			tw (scatter far_area_acre_ gps_area_old_ if trt2==`i' & year==201`j', xsc(r(0(5)15))) /// 
			   (function y=x, range(0 15) lcolor("165 165 165") lpattern("l") lwidth(0.25)) , /// 
			   name(fig_`i'_`j', replace) legend(off) title("Control (201`j')") /// 
			   xti("GPS") yti("Self-Reported") yla(, nogrid) xla(, nogrid) ///
			   note("Pearson's Correlation=`rho'", ring(0) pos(1) size(*1.1) box)	
			}
		else if `i'==2 { 
			correlate far_area_acre_ gps_area_old_ if trt2==`i' & year==201`j'
			loc rho: di %5.2f r(rho) 
			tw (scatter far_area_acre_ gps_area_old_ if trt2==`i' & year==201`j', xsc(r(0(5)15))) /// 
			   (function y=x, range(0 15) lcolor("165 165 165") lpattern("l") lwidth(0.25)) , /// 
			   name(fig_`i'_`j', replace) legend(off) title("Voucher") /// 
			   xti("GPS") yti("Self-Reported") yla(, nogrid) xla(, nogrid) ///
			   note("Pearson's Correlation=`rho'", ring(0) pos(1) size(*1.1) box)	
			}
		else if `i'==3 { 
			correlate far_area_acre_ gps_area_old_ if trt2==`i' & year==201`j'
			loc rho: di %5.2f r(rho) 
			tw (scatter far_area_acre_ gps_area_old_ if trt2==`i' & year==201`j', xsc(r(0(5)15))) /// 
			   (function y=x, range(0 15) lcolor("165 165 165") lpattern("l") lwidth(0.25)) , /// 
			   name(fig_`i'_`j', replace) legend(off) title("Recommendations") /// 
			   xti("GPS") yti("Self-Reported") yla(, nogrid) xla(, nogrid) ///
			   note("Pearson's Correlation=`rho'", ring(0) pos(1) size(*1.1) box)	 	
			}
		else if `i'==4 { 
			correlate far_area_acre_ gps_area_old_ if trt2==`i' & year==201`j'
			loc rho: di %5.2f r(rho) 
			tw (scatter far_area_acre_ gps_area_old_ if trt2==`i' & year==201`j', xsc(r(0(5)15))) /// 
			   (function y=x, range(0 15) lcolor("165 165 165") lpattern("l") lwidth(0.25)) , /// 
			   name(fig_`i'_`j', replace) legend(off) title("Voucher+Recommendations") /// 
			   xti("GPS") yti("Self-Reported") yla(, nogrid) xla(, nogrid) ///
			   note("Pearson's Correlation=`rho'", ring(0) pos(1) size(*1.1) box)	 	
				}
			}
		}
		gr combine fig_1_4 fig_1_6 fig_1_9 /// 
		           fig_2_4 fig_2_6 fig_2_9 ///
				   fig_3_4 fig_3_6 fig_3_9 ///
				   fig_4_4 fig_4_6 fig_4_9, /// 
				   cols(3) ysize(20) xsize(15)
			gr export "$path/replication/output/Figure2.pdf", as(pdf) replace
	restore
	
end


program CrossVillageITT

	mat A_6 = J(5,1,.) 
	mat A_9 = J(5,1,.) 
	mat B_6 = J(5,1,.) 
	mat B_9 = J(5,1,.) 
	mat C_6 = J(5,1,.) 
	mat C_9 = J(5,1,.) 
	mat D_6 = J(5,1,.) 
	mat D_9 = J(5,1,.) 
	
	forval i=6/9 {
	if inlist(`i', 7, 8) continue
	***A. 2014 MMP
	xtreg far_mz_kg_acre_q30_ i.trt2 fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& year==201`i', fe robust cl(vil) 
			outreg2 using "${path}/replication/output/TableA18.doc", append /// 
			ctitle(fr`i') bd(2) sd(2) nocons addtext(Village FE, YES)
        su fr_baseline if e(sample) & trt2 == 1		
	 	mat A_`i'[1,1] = r(mean)
		mat A_`i'[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat A_`i'[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat A_`i'[4,1] = r(p)
		test 3.trt2=4.trt2
		mat A_`i'[5,1] = r(p)
		mat list A_`i'
		
	xtreg gps_mz_kg_acre_q30_ i.trt2 gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& year==201`i', fe robust cl(vil) 
			outreg2 using "${path}/replication/output/TableA18.doc", append /// 
			ctitle(gps`i') bd(2) sd(2) nocons addtext(Village FE, YES)	
        su gps_baseline if e(sample) & trt2 == 1		
		mat B_`i'[1,1] = r(mean)	
		mat B_`i'[2,1] = r(sd)
	 	test 2.trt2=3.trt2
		mat B_`i'[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat B_`i'[4,1] = r(p)
		test 3.trt2=4.trt2
		mat B_`i'[5,1] = r(p)
		mat list B_`i'

	xtreg far_mz_kg_acre_q30_ i.trt2 fr_baseline if gps_mz_kg_acre_q30_!=. & gps_baseline!=. /// 
		& gps_area_old_>0.5 & year==201`i', fe robust cl(vil) 
			outreg2 using "${path}/replication/output/TableA18.doc", append /// 
			ctitle(fr`i'0.5) bd(2) sd(2) nocons addtext(Village FE, YES)
        su fr_baseline if e(sample) & trt2 == 1		
		mat C_`i'[1,1] = r(mean)	
		mat C_`i'[2,1] = r(sd)	
	 	test 2.trt2=3.trt2	
		mat C_`i'[3,1] = r(p)  	
		test 2.trt2=4.trt2	
		mat C_`i'[4,1] = r(p)	
		test 3.trt2=4.trt2	
		mat C_`i'[5,1] = r(p)	
		mat list C_`i'	
			
	xtreg gps_mz_kg_acre_q30_ i.trt2 gps_baseline if far_mz_kg_acre_q30_!=. & fr_baseline!=. /// 
		& gps_area_old_>0.5 & year==201`i', fe robust cl(vil) 
			outreg2 using "${path}/replication/output/TableA18.doc", append /// 
			ctitle(gps`i'0.5) bd(2) sd(2) nocons addtext(Village FE, YES)
        su gps_baseline if e(sample) & trt2 == 1		
		mat D_`i'[1,1] = r(mean)	
		mat D_`i'[2,1] = r(sd)	
	 	test 2.trt2=3.trt2	
		mat D_`i'[3,1] = r(p)  	
		test 2.trt2=4.trt2	
		mat D_`i'[4,1] = r(p)	
		test 3.trt2=4.trt2	
		mat D_`i'[5,1] = r(p)	
		mat list D_`i'	
	}		
	
	*Export
	matmap A_6 A_6N, map(round(@,0.01))
	matmap A_9 A_9N, map(round(@,0.01))
	matmap B_6 B_6N, map(round(@,0.01))
	matmap B_9 B_9N, map(round(@,0.01))
	matmap C_6 C_6N, map(round(@,0.01))
	matmap C_9 C_9N, map(round(@,0.01))
	matmap D_6 D_6N, map(round(@,0.01))
	matmap D_9 D_9N, map(round(@,0.01))
	
	putexcel set "${path}/replication/output/TableA18_F_tests.xls", sheet("F-tests") replace
	putexcel B2=matrix(A_6N) 
	putexcel C2=matrix(B_6N)
	putexcel D2=matrix(A_9N)
	putexcel E2=matrix(B_9N)		
	putexcel F2=matrix(C_6N) 
	putexcel G2=matrix(D_6N)
	putexcel H2=matrix(C_9N)
	putexcel I2=matrix(D_9N)
	
	putexcel B1 = "SR_16"
	putexcel C1 = "GPS_16"
	putexcel D1 = "SR_19"
	putexcel E1 = "GPS_19"
	putexcel F1 = "SR_16"
	putexcel G1 = "GPS_16"
	putexcel H1 = "SR_19"
	putexcel I1 = "GPS_19"
	putexcel A2 = "CONTROL MEAN"
	putexcel A3 = "CONTROL SD"
	putexcel A4 = "V_vs_R"
	putexcel A5 = "V_vs_RV"
	putexcel A6 = "R_vs_RV"
	putexcel A7 = "less than 0.5 acres"
	putexcel B7 = "NO"
	putexcel C7 = "NO"
	putexcel D7 = "NO"
	putexcel E7 = "NO"
	putexcel F7 = "YES"
	putexcel G7 = "YES"
	putexcel H7 = "YES"
	putexcel I7 = "YES"
	
	erase "${path}/replication/output/TableA18.txt"

end 

						
*Execute												
main												
												
*EOF												
